#include <exception>

#include "ConstraintTable.h"


ConstraintTable::ConstraintTable(void) {
}


ConstraintTable::~ConstraintTable(void) {
}

bool ConstraintTable::replaceSynonym(std::string s1, std::string s2) {
	ConstraintEntry e ;
	for (int i = 0; i<mConstraintEntries.size();i++) {
		e = mConstraintEntries.at(i);
		for (int j = 0; j<=1; j++) {
			if (e.constraintArguments[j].compare(s2) == 0 && e.constraintArgumentTypes[j] != STRING && e.constraintArgumentTypes[j] != INTEGER) {
				mConstraintEntries.at(i).constraintArguments[j] = s1;
			}
		}
	}	
	return true;
}

bool ConstraintTable::addConstraintEntry(ConstraintType relationshipType, 
	std::string constraintArg1, RelationshipArgumentType relationshipArgType1, ConstraintAttrType attr1,
	std::string constraintArg2, RelationshipArgumentType relationshipArgType2, ConstraintAttrType attr2) {
		// Error checking
		if (relationshipType == NULL) {
			return false;
		}
		if (constraintArg1 == "" || constraintArg2 == "") {
			return false;
		}
		if (relationshipArgType1 == NULL || relationshipArgType2 == NULL) {
			return false;
		}

		ConstraintEntry entry;
		entry.constraintType = relationshipType;
		entry.constraintArguments[0] = constraintArg1;
		entry.constraintArguments[1] = constraintArg2;
		entry.constraintArgumentTypes[0] = relationshipArgType1;
		entry.constraintArgumentTypes[1] = relationshipArgType2;
		entry.constraintAttrTypes[0] = attr1;
		entry.constraintAttrTypes[1] = attr2;

		entry.numberOfArguments = 2;

		mConstraintEntries.push_back(entry);
		return true;
}

ConstraintEntry ConstraintTable::getConstraintEntry(int index) {
	if (index < 0 || index >= getNumberOfEntries()) {
		ConstraintEntry entry;
		return entry;
	}
	return mConstraintEntries.at(index);
}

int ConstraintTable::getNumberOfEntries() {
	return mConstraintEntries.size();
}

bool ConstraintTable::removeEntryAtIndex(int index) {
	if (index < 0 || index >= getNumberOfEntries()) {
		return false;
	}
	mConstraintEntries.erase(mConstraintEntries.begin()+index);
	return true;
}